﻿using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TFSUtilLibrary
{
    public class CleanWorkspace
    {       
        public static List<string> GetWorkspacesWithNoPendingChanges(string uri)
        {
            Connection con = new Connection(uri);
            List<string> wsList = new List<string>();

            if (!con.ConnectToTFS())
                Common.Messages.ThrowNonConnect();

            Workspace[] ws = con.VCS.QueryWorkspaces(null, null, null);

            foreach (Workspace s in ws)
            {
                var pend = s.GetPendingChanges();
                if (pend.Count() == 0)
                {
                    wsList.Add(s.Name);                                    
                    continue;
                }
            }
            return wsList;
        }

        /// <summary>
        /// Remove the list of workspaces and return a list of all that are removed
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="workspaceList"></param>
        /// <returns></returns>
        public static List<string> RemoveWorkspaces(string uri, List<string> workspaceList)
        {
            List<string> wsList = new List<string>();
            Connection con = new Connection(uri);
            if (!con.ConnectToTFS())
                Common.Messages.ThrowNonConnect();

            var service = con.VCS;            

            Workspace[] ws = service.QueryWorkspaces(null, null, null);

            foreach (Workspace s in ws.Where(w => workspaceList.Contains(w.Name)))
            {
                s.Delete();
                wsList.Add(s.Name);
            }

            return wsList;
        }
    }
}
